// 引入样式
import "../../assets/css/normalize.less";
import "../../assets/css/reset.less";
import "../../assets/css/property.less";
import "../../assets/css/basic.less";
import "./login.less";
import { $ } from "../../utils/dom";
import "../../api/user";

// 引入字体图标
import "../../assets/fonts/font_kok358gumca/iconfont.css";

// 引入请求接口
import { postLoginReq } from "../../api/user";

// 引入轻提示
import { toast } from "../../utils/toast";

// 验证手机号
$("#btn").addEventListener("click", async () => {
	if (!/^1[3,9]\d{9}$/.test($("#num").value)) {
		$("#tips").innerHTML = "手机号格式错误";
		return;
	}
	// 验证密码
	if (!/^\d{6}$/.test($("#psd1").value)) {
		$("#tips").innerHTML = "请输入六位密码";
		return;
	}

	// 校验成功
	$("#tips").innerHTML = "";

	// 发请求
	let res = await postLoginReq({
		account: $("#num").value,
		password: $("#psd1").value,
	});

	// 成功解构响应对象
	let { errno, data } = res.data;
	console.log(data);
	//判断errno 的状态
	if (errno === 0) {
		// 成功
		sessionStorage.setItem("token", data.token);
		toast("登录成功");
		setTimeout(() => {
			location.href = "./home.html";
		}, 4000);
		// 失败
	} else {
		toast("登录失败");
	}
});
